#include<iostream>
#include<queue>
#include<vector>

using namespace std;

typedef long long ll;
priority_queue<ll, vector<ll>, greater<ll>> q;

int main()
{
	int n; cin>>n;
	while(n--)
	{
		ll x; cin>>x;
		q.push(x);
	}
	
	ll ret=0;
	while(q.size()>1)
	{
		ll x=q.top(); q.pop();
		ll y=q.top(); q.pop();
		ret+=(x+y);
		q.push(x+y);
	}
	cout<<ret 
	return 0;
 } 
